package com.liuguang.framework.utils;

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;

public class FileUtil {
    public static boolean existSDCard() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static String getNameFromUrl(String str) {
        if (TextUtils.isEmpty(str)) {
            return null;
        }
        String[] split = str.split("/");
        return split[split.length - 1];
    }

    public static File createFile(String str, String str2) {
        File file = new File(str);
        if (!file.exists()) {
            file.mkdirs();
        }
        File file2 = new File(file, str2);
        if (!file2.exists()) {
            try {
                file2.createNewFile();
            } catch (IOException unused) {
                Log.e("zp_test", "文件创建失败！");
            }
        }
        return file2;
    }

    public static File createFile(File file, String str, String str2) {
        if (!file.exists() || !file.isDirectory()) {
            file.mkdirs();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.CHINA);
        StringBuilder sb = new StringBuilder();
        sb.append(str);
        sb.append(simpleDateFormat.format(new Date(System.currentTimeMillis())));
        sb.append(str2);
        return new File(file, sb.toString());
    }

    public static void orderByDate(File[] fileArr) {
        Arrays.sort(fileArr, new Comparator<File>() {
            public boolean equals(Object obj) {
                return true;
            }

            public int compare(File file, File file2) {
                long lastModified = file.lastModified() - file2.lastModified();
                if (lastModified > 0) {
                    return 1;
                }
                return lastModified == 0 ? 0 : -1;
            }
        });
    }

    public static boolean deleteFile(String str) {
        return deleteFile(new File(str));
    }

    public static boolean deleteFile(File file) {
        if (!file.exists()) {
            return true;
        }
        if (file.isDirectory()) {
            for (File deleteFile : file.listFiles()) {
                if (!deleteFile(deleteFile)) {
                    return false;
                }
            }
        }
        return file.delete();
    }

    public static void copyDir(String str, String str2) throws IOException {
        String[] list = new File(str).list();
        if (!new File(str2).exists()) {
            new File(str2).mkdir();
        }
        for (int i = 0; i < list.length; i++) {
            StringBuilder sb = new StringBuilder();
            sb.append(str);
            sb.append(File.separator);
            sb.append(list[i]);
            if (new File(sb.toString()).isDirectory()) {
                StringBuilder sb2 = new StringBuilder();
                sb2.append(str);
                sb2.append(File.separator);
                sb2.append(list[i]);
                String sb3 = sb2.toString();
                StringBuilder sb4 = new StringBuilder();
                sb4.append(str2);
                sb4.append(File.separator);
                sb4.append(list[i]);
                copyDir(sb3, sb4.toString());
            }
            StringBuilder sb5 = new StringBuilder();
            sb5.append(str);
            sb5.append(File.separator);
            sb5.append(list[i]);
            if (new File(sb5.toString()).isFile()) {
                StringBuilder sb6 = new StringBuilder();
                sb6.append(str);
                sb6.append(File.separator);
                sb6.append(list[i]);
                String sb7 = sb6.toString();
                StringBuilder sb8 = new StringBuilder();
                sb8.append(str2);
                sb8.append(File.separator);
                sb8.append(list[i]);
                copyFile(sb7, sb8.toString());
            }
        }
    }

    public static void copyFile(String str, String str2) throws IOException {
        File file = new File(str);
        File file2 = new File(str2);
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(file2);
        byte[] bArr = new byte[2097152];
        while (true) {
            int read = fileInputStream.read(bArr);
            if (read != -1) {
                fileOutputStream.write(bArr, 0, read);
            } else {
                fileInputStream.close();
                fileOutputStream.close();
                return;
            }
        }
    }
}
